package com.his.dao;

import com.his.bean.DrugBean;
import com.his.bean.PrescriptionBean;
import com.his.bean.SymptomsBean;
import com.his.common.JDBCSingleton;
import com.his.common.StringPolicy;
import com.his.common.SystemSequences;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;

@SuppressWarnings("all")
public class DaoImpl implements DaoInterface {
	public List getDrugList() {
		Connection conn = JDBCSingleton.getInstance().getConnection();
		String sql = "select * from drug order by drug_name,drug_abbr";
		PreparedStatement ps = null;
		ResultSet rs = null;
		List list = new ArrayList();
		try {
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		try {
			while (rs.next()) {
				DrugBean drugBo = new DrugBean();
				drugBo.setId(Long.valueOf(rs.getLong(1)));
				drugBo.setDrugAbbr(rs.getString(2));
				drugBo.setDrugName(rs.getString(3));
				drugBo.setDrugDesc(rs.getString(4));
				drugBo.setUnitPrice(Double.valueOf(rs.getDouble(5)));
				drugBo.setInvQty(Double.valueOf(rs.getDouble(6)));
				list.add(drugBo);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (rs != null)
					rs.close();
				if (ps != null)
					ps.close();
				if (conn != null)
					conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return list;
	}

	public DrugBean getDrugByAbbr(String strAbbr) {
		if (StringPolicy.isNull(strAbbr)) {
			return null;
		}
		Connection conn = JDBCSingleton.getInstance().getConnection();
		DrugBean drugBo = null;
		String s1 = "select * from drug where drug_abbr =?";
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn.prepareStatement(s1);
			ps.setString(1, strAbbr);
			rs = ps.executeQuery();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		try {
			while (rs.next()) {
				drugBo = new DrugBean();
				drugBo.setId(Long.valueOf(rs.getLong(1)));
				drugBo.setDrugAbbr(rs.getString(2));
				drugBo.setDrugName(rs.getString(3));
				drugBo.setDrugDesc(rs.getString(4));
				drugBo.setUnitPrice(Double.valueOf(rs.getDouble(5)));
				drugBo.setInvQty(Double.valueOf(rs.getDouble(6)));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (rs != null)
					rs.close();
				if (ps != null)
					ps.close();
				if (conn != null)
					conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return drugBo;
	}

	public boolean insert(DrugBean drug) {
		if (drug == null) {
			return true;
		}
		Connection conn = JDBCSingleton.getInstance().getConnection();
		String sql = "";
		PreparedStatement ps = null;
		try {
			String drugAbbr = drug.getDrugAbbr();
			String drugName = drug.getDrugName();
			String drugDesc = drug.getDrugDesc();
			Double unitPrice = drug.getUnitPrice();
			Double invQty = drug.getInvQty();
			Long id = SystemSequences.getSystemSequences();
			java.sql.Date date = new java.sql.Date(System.currentTimeMillis());
			sql = "insert into drug(id,drug_abbr,drug_name,drug_desc,unit_price,inv_qty,timestamp) values(?,?,?,?,?,?,?)";

			ps = conn.prepareStatement(sql);
			ps.setLong(1, id.longValue());
			ps.setString(2, drugAbbr);
			ps.setString(3, drugName);
			ps.setString(4, drugDesc);
			if (unitPrice != null)
				ps.setDouble(5, unitPrice.doubleValue());
			else {
				ps.setNull(5, 8);
			}
			if (invQty != null)
				ps.setDouble(6, invQty.doubleValue());
			else {
				ps.setNull(6, 8);
			}
			ps.setDate(7, date);
			ps.executeUpdate();
			return true;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		} finally {
			try {
				if (ps != null) {
					ps.close();
				}
				if (conn != null)
					conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	public boolean deleteDrugList(List list) {
		String str = "delete from drug where id=?";
		Long id = null;
		Iterator it = list.iterator();
		PreparedStatement ps = null;
		if ((list == null) || (list.size() < 1)) {
			return false;
		}
		Connection conn = JDBCSingleton.getInstance().getConnection();
		while (true)
			try {
				while (it.hasNext()) {
					id = ((DrugBean) it.next()).getId();
					if (id == null) {
						return false;
					}
					ps = conn.prepareStatement(str);
					ps.setLong(1, id.longValue());
					ps.executeUpdate();
				}
				return true;
			} catch (SQLException e) {
			} finally {
				try {
					if (ps != null) {
						ps.close();
					}
					if (conn != null)
						conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
	}

	public boolean update(DrugBean drugUpdate) {
		PreparedStatement ps = null;
		Connection conn = JDBCSingleton.getInstance().getConnection();
		if ((drugUpdate == null) || (drugUpdate.getId() == null)
				|| (drugUpdate.getId().longValue() <= 0L))
			return true;
		try {
			String sql = "update drug set drug_name=?,drug_abbr=?,drug_desc=?,unit_price=?,inv_qty=? where id=?";
			ps = conn.prepareStatement(sql);
			ps.setString(1, drugUpdate.getDrugName());
			ps.setString(2, drugUpdate.getDrugAbbr());
			ps.setString(3, drugUpdate.getDrugDesc());
			if (drugUpdate.getUnitPrice() != null)
				ps.setDouble(4, drugUpdate.getUnitPrice().doubleValue());
			else {
				ps.setNull(4, 8);
			}
			if (drugUpdate.getInvQty() != null)
				ps.setDouble(5, drugUpdate.getInvQty().doubleValue());
			else {
				ps.setNull(5, 8);
			}
			ps.setLong(6, drugUpdate.getId().longValue());
			ps.executeUpdate();
			return true;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		} finally {
			try {
				if (ps != null) {
					ps.close();
				}
				if (conn != null)
					conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	public boolean deleteSymptomsList(List list) {
		Connection conn = JDBCSingleton.getInstance().getConnection();
		String sql1 = "delete from prescription where id_parent=?";
		String sql2 = "delete from symptoms where id=?";
		Long id = null;
		Iterator it = list.iterator();
		PreparedStatement ps = null;
		if ((list == null) || (list.size() < 1))
			return false;
		while (true)
			try {
				while (it.hasNext()) {
					id = ((SymptomsBean) it.next()).getId();
					if (id == null) {
						return false;
					}
					ps = conn.prepareStatement(sql1);
					ps.setLong(1, id.longValue());
					ps.executeUpdate();

					ps.clearParameters();
					ps = conn.prepareStatement(sql2);
					ps.setLong(1, id.longValue());
					ps.executeUpdate();
				}
				return true;
			} catch (SQLException e) {
			} finally {
				try {
					if (ps != null) {
						ps.close();
					}
					if (conn != null)
						conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
	}

	public List getSymptomsList(SymptomsBean searchBean) {
		Connection conn = JDBCSingleton.getInstance().getConnection();
		String searchName = null;
		java.util.Date searchDateFrom = null;
		java.util.Date searchDateTo = null;
		String searchDesc = null;
		if (searchBean != null) {
			searchName = searchBean.getPatientName();
			searchDateFrom = searchBean.getSearchDateFrom();
			searchDateTo = searchBean.getSearchDateTo();
			searchDesc = searchBean.getSymptoms();
		}
		StringBuffer sql = new StringBuffer();
		List listParam = new ArrayList();
		sql.append("select * from symptoms where 1=1");
		if (!StringPolicy.isNull(searchName)) {
			sql.append(" and patient_name like ?");
			listParam.add("%" + searchName + "%");
		}
		if ((searchDateFrom != null) && (searchDateTo == null)) {
			searchDateFrom = truncDate(searchDateFrom);
			sql.append(" and prescription_date >= ?");
			listParam.add(searchDateFrom);
		} else if ((searchDateTo != null) && (searchDateFrom == null)) {
			searchDateTo = truncDate(addDate(searchDateTo, 1));
			sql.append(" and prescription_date < ?");
			listParam.add(searchDateTo);
		} else if ((searchDateFrom != null) && (searchDateTo != null)) {
			searchDateFrom = truncDate(searchDateFrom);
			searchDateTo = truncDate(addDate(searchDateTo, 1));
			sql.append(" and prescription_date >= ?");
			sql.append(" and prescription_date < ?");
			listParam.add(searchDateFrom);
			listParam.add(searchDateTo);
		}
		if (!StringPolicy.isNull(searchDesc)) {
			sql.append(" and symptoms like ?");
			listParam.add("%" + searchDesc + "%");
		}
		sql.append(" order by prescription_date,id");

		String s1 = sql.toString();

		PreparedStatement ps = null;
		ResultSet rs = null;
		List list = new ArrayList();
		try {
			ps = conn.prepareStatement(s1);
			if ((listParam != null) && (listParam.size() > 0)) {
				for (int i = 0; i < listParam.size(); ++i) {
					Object obj = listParam.get(i);
					if (obj instanceof String)
						ps.setString(i + 1, obj.toString());
					else if (obj instanceof java.util.Date) {
						ps.setDate(i + 1, new java.sql.Date(
								((java.util.Date) obj).getTime()));
					}
				}
			}
			rs = ps.executeQuery();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		try {
			while (rs.next()) {
				SymptomsBean symptomsBo = new SymptomsBean();
				symptomsBo.setId(Long.valueOf(rs.getLong(1)));
				symptomsBo.setPatientName(rs.getString(2));
				symptomsBo.setSymptoms(rs.getString(3));
				symptomsBo.setPrescriptionDate(rs.getDate(4));
				list.add(symptomsBo);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (rs != null)
					rs.close();
				if (ps != null)
					ps.close();
				if (conn != null)
					conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return list;
	}

	public SymptomsBean getSymptomsById(Long id) {
		Connection conn = JDBCSingleton.getInstance().getConnection();
		if ((id == null) || (id.longValue() <= 0L)) {
			return null;
		}
		SymptomsBean result = null;
		String sql = "select * from symptoms where id=?";
		PreparedStatement ps = null;
		ResultSet rs = null;
		List list = new ArrayList();
		try {
			ps = conn.prepareStatement(sql);
			ps.setLong(1, id.longValue());
			rs = ps.executeQuery();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		try {
			while (rs.next()) {
				result = new SymptomsBean();
				result.setId(Long.valueOf(rs.getLong(1)));
				result.setPatientName(rs.getString(2));
				result.setSymptoms(rs.getString(3));
				result.setPrescriptionDate(rs.getDate(4));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (rs != null)
					rs.close();
				if (ps != null)
					ps.close();
				if (conn != null)
					conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return result;
	}

	public boolean insert(SymptomsBean symptoms) {
		Connection conn = JDBCSingleton.getInstance().getConnection();
		if (symptoms == null) {
			return true;
		}
		String sql = "";
		PreparedStatement ps = null;
		try {
			String name = symptoms.getPatientName();
			String desc = symptoms.getSymptoms();
			java.util.Date prescriptionDate = symptoms.getPrescriptionDate();
			Long id = symptoms.getId();
			java.sql.Date date = new java.sql.Date(System.currentTimeMillis());
			sql = "insert into symptoms(id,patient_name,symptoms,prescription_date,timestamp) values(?,?,?,?,?)";

			ps = conn.prepareStatement(sql);
			ps.setLong(1, id.longValue());
			ps.setString(2, name);
			ps.setString(3, desc);
			ps.setDate(4, new java.sql.Date(prescriptionDate.getTime()));
			ps.setDate(5, date);
			ps.executeUpdate();
			return true;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		} finally {
			try {
				if (ps != null) {
					ps.close();
				}
				if (conn != null)
					conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	public boolean update(SymptomsBean symptomsUpdate) {
		Connection conn = JDBCSingleton.getInstance().getConnection();
		PreparedStatement ps = null;
		if ((symptomsUpdate == null) || (symptomsUpdate.getId() == null)
				|| (symptomsUpdate.getId().longValue() <= 0L))
			return true;
		try {
			String sql = "update symptoms set patient_name=?,symptoms=?,prescription_date=? where id=?";
			ps = conn.prepareStatement(sql);
			ps.setString(1, symptomsUpdate.getPatientName());
			ps.setString(2, symptomsUpdate.getSymptoms());
			ps.setDate(3, new java.sql.Date(symptomsUpdate
					.getPrescriptionDate().getTime()));
			ps.setLong(4, symptomsUpdate.getId().longValue());
			ps.executeUpdate();
			return true;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		} finally {
			try {
				if (ps != null) {
					ps.close();
				}
				if (conn != null)
					conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	private java.util.Date truncDate(java.util.Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(11, 0);
		calendar.set(12, 0);
		calendar.set(13, 0);
		calendar.set(14, 0);
		return calendar.getTime();
	}

	private java.util.Date addDate(java.util.Date date, int days) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(5, days);
		return calendar.getTime();
	}

	public boolean deletePrescriptionList(List list) {
		Connection conn = JDBCSingleton.getInstance().getConnection();
		String sql = "delete from prescription where id=?";
		Long id = null;
		Iterator it = list.iterator();
		PreparedStatement ps = null;
		if ((list == null) || (list.size() < 1))
			return false;
		while (true)
			try {
				while (it.hasNext()) {
					id = ((PrescriptionBean) it.next()).getId();
					if (id == null) {
						return false;
					}
					ps = conn.prepareStatement(sql);
					ps.setLong(1, id.longValue());
					ps.executeUpdate();
				}
				return true;
			} catch (SQLException e) {
			} finally {
				try {
					if (ps != null) {
						ps.close();
					}
					if (conn != null)
						conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
	}

	public List getPrescriptionList(PrescriptionBean searchBean) {
		if ((searchBean == null) || (searchBean.getIdParent() == null)
				|| (searchBean.getIdParent().longValue() <= 0L)) {
			return null;
		}
		Connection conn = JDBCSingleton.getInstance().getConnection();
		String s1 = "select * from prescription where id_parent=? order by id";
		PreparedStatement ps = null;
		ResultSet rs = null;
		List list = new ArrayList();
		try {
			ps = conn.prepareStatement(s1);
			ps.setLong(1, searchBean.getIdParent().longValue());
			rs = ps.executeQuery();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		try {
			while (rs.next()) {
				PrescriptionBean prescription = new PrescriptionBean();
				prescription.setId(Long.valueOf(rs.getLong(1)));
				prescription.setIdParent(Long.valueOf(rs.getLong(2)));
				prescription.setDrugAbbr(rs.getString(3));
				prescription.setDrugName(rs.getString(4));
				prescription.setUnitPrice(Double.valueOf(rs.getDouble(5)));
				prescription.setQty(Double.valueOf(rs.getDouble(6)));
				list.add(prescription);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (rs != null)
					rs.close();
				if (ps != null)
					ps.close();
				if (conn != null)
					conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return list;
	}

	public boolean insert(PrescriptionBean prescription) {
		if (prescription == null) {
			return true;
		}
		if ((prescription.getIdParent() == null)
				|| (prescription.getIdParent().longValue() <= 0L)) {
			return false;
		}
		Connection conn = JDBCSingleton.getInstance().getConnection();
		String sql = "";
		PreparedStatement ps = null;
		try {
			String name = prescription.getDrugName();
			String abbr = prescription.getDrugAbbr();
			Double unitPrice = prescription.getUnitPrice();
			Double qty = prescription.getQty();
			Long id = SystemSequences.getSystemSequences();
			Long idParent = prescription.getIdParent();
			java.sql.Date date = new java.sql.Date(System.currentTimeMillis());
			sql = "insert into prescription(id,id_parent,drug_abbr,drug_name,unit_price,qty,timestamp) values(?,?,?,?,?,?,?)";

			ps = conn.prepareStatement(sql);
			ps.setLong(1, id.longValue());
			ps.setLong(2, idParent.longValue());
			ps.setString(3, abbr);
			ps.setString(4, name);
			ps.setDouble(5, unitPrice.doubleValue());
			ps.setDouble(6, qty.doubleValue());
			ps.setDate(7, date);
			ps.executeUpdate();
			return true;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		} finally {
			try {
				if (ps != null) {
					ps.close();
				}
				if (conn != null)
					conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	public boolean insert(List<PrescriptionBean> list) {
		Connection conn = JDBCSingleton.getInstance().getConnection();
		if ((list == null) || (list.size() < 1)) {
			return false;
		}
		PreparedStatement ps = null;
		try {
			for (PrescriptionBean prescription : list) {
				String name = prescription.getDrugName();
				String abbr = prescription.getDrugAbbr();
				Double unitPrice = prescription.getUnitPrice();
				Double qty = prescription.getQty();
				Long id = SystemSequences.getSystemSequences();
				Long idParent = prescription.getIdParent();
				java.sql.Date date = new java.sql.Date(System
						.currentTimeMillis());
				String sql = "insert into prescription(id,id_parent,drug_abbr,drug_name,unit_price,qty,timestamp) values(?,?,?,?,?,?,?)";

				ps = conn.prepareStatement(sql);
				ps.setLong(1, id.longValue());
				ps.setLong(2, idParent.longValue());
				ps.setString(3, abbr);
				ps.setString(4, name);
				ps.setDouble(5, unitPrice.doubleValue());
				ps.setDouble(6, qty.doubleValue());
				ps.setDate(7, date);
				ps.executeUpdate();
				ps.clearParameters();
			}
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		} finally {
			try {
				if (ps != null) {
					ps.close();
				}
				if (conn != null)
					conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

		return true;
	}

	public boolean update(PrescriptionBean prescriptionUpdate) {
		Connection conn = JDBCSingleton.getInstance().getConnection();
		PreparedStatement ps = null;
		if ((prescriptionUpdate == null)
				|| (prescriptionUpdate.getId() == null)
				|| (prescriptionUpdate.getId().longValue() <= 0L))
			return true;
		try {
			String sql = "update prescription set drug_abbr=?,drug_name=?,unit_price=?,qty=? where id=?";
			ps = conn.prepareStatement(sql);
			ps.setString(1, prescriptionUpdate.getDrugAbbr());
			ps.setString(2, prescriptionUpdate.getDrugName());
			ps.setDouble(3, prescriptionUpdate.getUnitPrice().doubleValue());
			ps.setDouble(4, prescriptionUpdate.getQty().doubleValue());
			ps.setLong(5, prescriptionUpdate.getId().longValue());
			ps.executeUpdate();
			return true;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		} finally {
			try {
				if (ps != null) {
					ps.close();
				}
				if (conn != null)
					conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
}